<?php
namespace Admin\Controller;

class BikeController extends CommonController{
 	/*整车列表*/
    public function index(){
        $this->where = array();
        $this->where['type'] = '1';
        $this->where['delete_status'] = '1';
        I('get.keywords') && $this->where['number|name'] = array('like','%'.I('get.keywords').'%');
        $this->order = 'create_time desc';
        parent::index();
    }
    
    /*添加商品*/
    public function add(){
    	$currentModel = D(CONTROLLER_NAME);
    	if(IS_POST){
    		$data = I('post.info');
    		if(!$res = $currentModel->create($data)) $this->error($currentModel->getError());
    		$result = $currentModel->saveData($data);
    		$result === false ? $this->error('保存失败') : $this->success('保存成功');
    	}else{
    		$formData = array();
    		I('get.id') && $formData = $currentModel->where(array('id'=>(int)I('get.id')))->find();
    		$this->assign('formData',$formData);
    		$this->display();
    	}
    }
    /*创建二维码*/
    public function ewm(){
    	I('id') && $data = D(CONTROLLER_NAME)->where(array('id'=>(int)I('id')))->find();
    	if(empty($data['ewm_path'])){
    		$ewm_url = C('EWM_URL').'?id='.(int)I('id');
    		$file_name = $data['number'] ? $data['number'].'.png' : time().'.png';
    		$ewm_path = create_qrcode($ewm_url, $file_name);
    		D(CONTROLLER_NAME)->where(array('id'=>(int)I('id')))->save(array('ewm_path'=>$ewm_path));
    	}else{
    		$ewm_path = $data['ewm_path'];
    	}
    	$this->assign('ewm_path',$ewm_path);
    	$this->display();
    }
    /*下载二维码*/
    public function down_ewm() {
    	$filename = $_SERVER['DOCUMENT_ROOT'].I('ewm_path');
    	header('Content-Type:image/jpg'); //指定下载文件类型
    	header('Content-Disposition: attachment; filename="' . basename($filename) . '"'); //指定下载文件的描述
    	header('Content-Length:' . filesize($filename)); //指定下载文件的大小
    	readfile($filename);
    }
}
